//
//  FLMChatBottomView.m
//  FLMSuperS
//
//  Created by chenshan on 2016/10/9.
//  Copyright © 2016年 FaLaoMiaoNetworkTechnologyCo. All rights reserved.
//

#import "FLMChatBottomView.h"

@implementation FLMChatBottomView

- (instancetype)initWithFrame:(CGRect)frame {
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        
        _textField  = [[UITextField alloc] init];
        _textField.font = [UIFont systemFontOfSize:12.0];
        _textField.placeholder = @"输入留言";
        //_textField.delegate = self;
        _textField.backgroundColor = [UIColor colorWithHexString:@"f2f2f2"];
        [self addSubview:_textField];
        
        _sendBtn = [[UIButton alloc] init];
        [_sendBtn setTitle:@"发送" forState:UIControlStateNormal];
        _sendBtn.titleLabel.font = [UIFont systemFontOfSize:12.0];
        //_sendBtn.backgroundColor = [UIColor blueColor];
        _sendBtn.layer.cornerRadius = 2.0;
        [_sendBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
       // [_sendBtn setImage:[UIImage imageNamed:@"chat_send_bg"] forState:UIControlStateNormal];
        [_sendBtn setBackgroundImage:[UIImage imageNamed:@"chat_send_bg"] forState:UIControlStateNormal];
        [_sendBtn addTarget:self action:@selector(sendMessage:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:_sendBtn];
        
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillChangeFrame:) name:UIKeyboardWillChangeFrameNotification object:nil];
    }
    
    return self;
}

#pragma mark - UIKeyboardNotification

- (void)keyboardWillChangeFrame:(NSNotification *)notification
{
    NSDictionary *userInfo = notification.userInfo;
    CGRect endFrame = [userInfo[UIKeyboardFrameEndUserInfoKey] CGRectValue];
    CGRect beginFrame = [userInfo[UIKeyboardFrameBeginUserInfoKey] CGRectValue];
    CGFloat duration = [userInfo[UIKeyboardAnimationDurationUserInfoKey] doubleValue];
    UIViewAnimationCurve curve = [userInfo[UIKeyboardAnimationCurveUserInfoKey] integerValue];
    
    CGFloat height = beginFrame.origin.y - endFrame.origin.y;
    void(^animations)() = ^{
        
        self.bottom -= height;
        if (_delegate && [_delegate respondsToSelector:@selector(didChangeFrameToHeight:)]) {
            [_delegate didChangeFrameToHeight:height];
        }
    };
    
    void(^completion)(BOOL) = ^(BOOL finished){
    };
    
    [UIView animateWithDuration:duration delay:0.0f options:(curve << 16 | UIViewAnimationOptionBeginFromCurrentState) animations:animations completion:completion];
}

- (void)textFieldDidBeginEditing:(UITextField *)textField {
    if([self.delegate respondsToSelector:@selector(chatBottomViewWillBeginSend)]) {
        //[self.delegate chatBottomViewWillBeginSend];
    }
}
//
//- (void)textFieldDidEndEditing:(UITextField *)textField {
//    if([self.delegate respondsToSelector:@selector(chatBottomViewDidEndSend)]) {
//        [self.delegate chatBottomViewDidEndSend];
//    }
//}

- (void)sendMessage:(UIButton *)sender {
    if([self.delegate respondsToSelector:@selector(chatBottomViewDidSendMessage:)] && self.textField.text.length > 0) {
        [self.delegate chatBottomViewDidSendMessage:self.textField.text];
        
        self.textField.text = @"";
    }
}

- (void)layoutSubviews {
    [super layoutSubviews];
    
    _sendBtn.top = 5;
    _sendBtn.width = 40;
    _sendBtn.height = self.height - 10;
    _sendBtn.right = self.width - 10;
    
    _textField.left = 30;
    _textField.top = _sendBtn.top;
    _textField.height = _sendBtn.height;
    _textField.width = _sendBtn.left - 30 - 5;
}

@end
